﻿using CAPlugin.Interface;
using SVS_S_SDK_COMLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Configuration;

namespace CAPlugin.Implement
{
    public class Server : IServer
    {
        private CSVS_S_SDK serverSDK = null;
        private CSVS_S_SDK tssSDK = null;

        public Server() 
        { 
            serverSDK = new CSVS_S_SDKClass();
            tssSDK = new CSVS_S_SDK();
        }

        public CSVS_S_SDK TssSDK
        {
            get { return tssSDK; }
        }

        public string Base64Encode(string inData)
        {
            return serverSDK.SOF_Base64Encode(inData);
        }

        public string CreateTimeStampResponse(string inData)
        {
           return serverSDK.SOF_CreateTimeStampResponse(inData);
        }

        public string DecryptData(string certAlias, string inData)
        {
            return serverSDK.SOF_DecryptData(certAlias, inData);
        }

        public string DecryptEnvelope(string inData)
        {
           return serverSDK.SOF_DecryptEnvelope(inData);
        }

        public int DecryptEnvelopeFromFile(string inFile, string outFile)
        {
           return serverSDK.SOF_DecryptEnvelopeFromFile(outFile, inFile);
        }

        public int DecryptFile(string certAlias, string inFile, string outFile)
        {
            return serverSDK.SOF_DecryptFile(certAlias, inFile, outFile);
        }

        public string DecryptSignedEnvelope(string inData)
        {
            return serverSDK.SOF_DecryptSignedEnvelope(inData);
        }

        public int DecryptSignedEnvelopeFromFile(string inFile, string outFile)
        {
           return serverSDK.SOF_DecryptSignedEnvelopeFromFile(outFile, inFile);
        }

        public string EncryptData(string certAlias, string inData)
        {
            return serverSDK.SOF_EncryptData(certAlias, inData);
        }

        public string EncryptEnvelope(string certAlias, string inData)
        {
           return serverSDK.SOF_EncryptEnvelope(certAlias, inData);
        }

        public int EncryptEnvelopeFromFile(string certAlias, string inFile, string outFile)
        {
            return serverSDK.SOF_EncryptEnvelopeFromFile(certAlias, inFile, outFile);
        }

        public int EncryptFile(string certAlias, string inFile, string outFile)
        {
           return serverSDK.SOF_EncryptFile(certAlias, inFile, outFile);
        }

        public string EncryptSignedEnvelope(string signedCertAlias, string encryptedCertAlias, string inData)
        {
            return serverSDK.SOF_EncryptSignedEnvelope(signedCertAlias, encryptedCertAlias, inData);
        }

        public int EncryptSignedEnvelopeFromFile(string signedCertAlias, string encyptedCertAlias, string inFile, string outFile)
        {
            return serverSDK.SOF_EncryptSignedEnvelopeFromFile(signedCertAlias, encyptedCertAlias, inFile, outFile);
        }

        public string GenRandom(short length)
        {
            return serverSDK.SOF_GenRandom(length);
        }

        public string GetCertInfo(string certAlias, short sType)
        {
            return serverSDK.SOF_GetCertInfo(certAlias, sType);
        }

        public string GetCertInfoByOid(string certAlias, string oid)
        {
            return serverSDK.SOF_GetCertInfoByOid(certAlias, oid);
        }

        public string GetErrorMsg()
        {
            return serverSDK.SOF_GetErrorMsg();
        }

        public string GetHashMethod()
        {
            return serverSDK.SOF_GetHashMethod();
        }

        public string GetP7SignDataInfo(string inData, short sType)
        {
            return serverSDK.SOF_GetP7SignDataInfo(inData, sType);
        }

        public string GetTimeStampData(string tsId)
        {
            return serverSDK.SOF_GetTimeStampData(tsId);
        }

        public string GetTimeStampInfo(string tsResponseData, short sType)
        {
            return serverSDK.SOF_GetTimeStampInfo(tsResponseData, sType);
        }

        public int P7SignFromFile(string certAlias, string inFile, string outFile)
        {
            return serverSDK.SOF_P7SignFromFile(certAlias, inFile, outFile);
        }

        public int P7VerifyFromFile(string inFile, string outFile)
        {
            return serverSDK.SOF_P7VerifyFromFile(inFile, outFile);
        }

        public string PriKeyDecrypt(string certAlias, string inData)
        {
            return serverSDK.SOF_PriKeyDecrypt(certAlias, inData);
        }

        public int PriKeyDecryptFromFile(string certAlias, string inFile, string outFile)
        {
            return serverSDK.SOF_PriKeyDecryptFromFile(certAlias, inFile, outFile);
        }

        public string PubKeyEncrypt(string certAlias, string inData)
        {
            return serverSDK.SOF_PubKeyEncrypt(certAlias, inData);
        }

        public int PubKeyEncryptFromFile(string certAlias, string inFile, string outFile)
        {
            return serverSDK.SOF_PubKeyEncryptFromFile(certAlias, inFile, outFile);
        }


       

        public int SetCertAlias(string signedCertAlias, string encryptedCertAlias)
        {
            return serverSDK.SOF_SetCertAlias(signedCertAlias, encryptedCertAlias);
        }

        public int SetEncryptMethod(string encryptMethod)
        {
            return serverSDK.SOF_SetEncryptMethod(encryptMethod);
        }

        public int SetHashMethod(string hashMethod)
        {
            return serverSDK.SOF_SetHashMethod(hashMethod);
        }

        public int SetP1VerifyMode(short sMode)
        {
            return serverSDK.SOF_SetP1VerifyMode(sMode);
        }

        public int SetP7SignMode(short sModel)
        {
            return serverSDK.SOF_SetP7SignMode((short)sModel);
        }

        public int SetServerInfo(string serverIP, int serverPort, string serverUri, int timeOut)
        {
            return serverSDK.SOF_SetServerInfo(serverIP, serverPort, serverUri, timeOut);   
        }

        public int SetTSASignMode(short sMode)
        {
            return serverSDK.SOF_SetTSASignMode((short)sMode);
        }

        public string SignData(string inData, int inDataLength)
        {
            return serverSDK.SOF_SignData(inData, inDataLength);
        }

        public string SignDataPkcs7(string inData)
        {
            return serverSDK.SOF_SignDataPkcs7(inData);
        }

        public string SignDataXML(string inData)
        {
            return serverSDK.SOF_SignDataXML(inData);
        }

        public string SignFile(string inFile)
        {
            return serverSDK.SOF_SignFile(inFile);
        }

        public string SOF_GetEncryptMethod()
        {
            return serverSDK.SOF_GetEncryptMethod();
        }

        public int ValidateCert(string certAlias)
        {
            return serverSDK.SOF_ValidateCert(certAlias);
        }

        public int VerifyDetachSignedData(string inData, string srcData)
        {
            return serverSDK.SOF_VerifyDetachSignedData(inData, srcData);
        }

        public int VerifySignedData(string certAlias, string inData, int inDataLength, string signValue)
        {
            return serverSDK.SOF_VerifySignedData(certAlias, inData, inDataLength, signValue);      
        }

        public string VerifySignedDataPkcs7(string inData)
        {
            return serverSDK.SOF_VerifySignedDataPkcs7(inData);
        }

        public int VerifySignedDataXML(string inData)
        {
            return serverSDK.SOF_VerifySignedDataXML(inData);
        }

        public int VerifySignedFile(string certAlias, string inFile, string signValue)
        {
            return serverSDK.SOF_VerifySignedFile(certAlias, inFile, signValue);
        }

        public string VerifyTimeStamp(string inData)
        {
            return serverSDK.SOF_VerifyTimeStamp(inData);
        }

        /// <summary>
        /// 服务器初始化
        /// </summary>
        /// <param name="errMsh"></param>
        /// <returns></returns>
        public bool ServerInit(out string errMsg)
        {
            int iRet = -1;
            bool isInit = false;
            errMsg = string.Empty;

            // 签名服务器
            string dssIp = ConfigurationManager.AppSettings["dss_IP"].ToString();
            int dssPort = Convert.ToInt32(ConfigurationManager.AppSettings["dss_PORT"]);
            string dssUri = ConfigurationManager.AppSettings["dss_URI"].ToString();
            int dssTimeOut = Convert.ToInt32(ConfigurationManager.AppSettings["dss_TIMEOUT"]);

            iRet = serverSDK.SOF_SetServerInfo(dssIp, dssPort, dssUri, dssTimeOut);

            if (iRet != 0)
            {
                errMsg = "签名服务器初始化失败！";
                return isInit;
            }
            else
            {
#if DEBUG
                Console.WriteLine("签名服务器初始化成功！");
#endif
            }

            // 时间戳服务器
            string tssIp = ConfigurationManager.AppSettings["tss_IP"].ToString();
            int tssPort = Convert.ToInt32(ConfigurationManager.AppSettings["tss_PORT"]);
            string tssUri = ConfigurationManager.AppSettings["tss_URI"].ToString();
            int tssTimeOut = Convert.ToInt32(ConfigurationManager.AppSettings["tss_TIMEOUT"]);

            iRet = tssSDK.SOF_SetServerInfo(tssIp, tssPort, tssUri, tssTimeOut);

            if (iRet != 0)
            {
                errMsg = "时间戳服务器初始化失败!";
                return isInit;
            }
            else
            {
#if DEBUG
                Console.WriteLine("时间戳服务器初始化成功！");
#endif
            }

            isInit = true;
            return isInit;
        }
    }
}
